<template>
    <el-form :model="companyFrom" :rules="companyFromRules" ref="companyFrom" label-width="100px" class="GO-company-form">
        <el-form-item label="公司名称" prop="name" class="form-item-number">
            <el-row>
                <el-col :span="11">
                    <el-input v-model="companyFrom.customerName"  placeholder="请填写公司名称"></el-input>
                </el-col>
            </el-row>
        </el-form-item>
        <el-form-item label="公司简称">
            <el-row>
                <el-col :span="11">
                    <el-input v-model="companyFrom.referred"  placeholder="请填写公司简称"></el-input>
                </el-col>
            </el-row>
        </el-form-item>
        <!--<el-form-item label="公司代码">
            <el-row>
                <el-col :span="11">
                    <el-input v-model="companyFrom.storeId" placeholder="公司代码自动获取简称首位字母"></el-input>
                </el-col>
            </el-row>
        </el-form-item>
        <el-form-item label="订单名称">
            <el-row>
                <el-col :span="11">
                    <el-input v-model="companyFrom.orderName" placeholder="请填写订单名称"></el-input>
                </el-col>
            </el-row>
        </el-form-item>-->
        <el-form-item label="公司备注" placeholder="请填写公备注">
            <el-row>
                <el-col :span="11">
                    <el-input type="textarea" rows="4"  v-model="companyFrom.remark"></el-input>
					<!-- :disabled="!canSave" -->
                </el-col>
            </el-row>
        </el-form-item>
        <el-form-item v-if="canSave">
            <el-button type="primary" @click="submitForm('companyFrom')" style="background: #2D2D2D;color: #e87114">保存</el-button>
        </el-form-item>
		<el-form-item v-if="!canSave">
		    <el-button type="primary" @click="updatecompany()" style="background: #2D2D2D;color: #e87114">保存</el-button>
		</el-form-item>
    </el-form>
</template>

<script>
    let storage = window.localStorage;
    import { addcustomer } from '@/api/group-order';
  import {
    deleteAction,
    getAction,
    putAction,
    postAction,
    downFile,
    getFileAccessHttpUrl
  } from '@/api/manage'
    export default {
        name: 'companyPane',
        data() {
            return {
				url:{
					updatecompany:"/company/updatecustomer",
					companydetail:"/company/gethmccustdetail",
				},
                companyFrom: {
                    name: '',
                    //公司名称
                    customerName: '',
                    //公司简称
                    referred: '',
                    //公司代码
                    customerCode: '',
                    //备注
                    remark: ''
                },
                // 是否为编辑状态
                canSave: true,
                // 表单验证
                companyFromRules: {
                    customerName: [{ required: true, message: '请填写公司名称', trigger: 'blur' }],
                    referred: [{ required: true, message: '请填写公司简称', trigger: 'blur' }],
                    codes: [{ required: true, message: '公司代码自动获取简称首位字母', trigger: 'blur' }],
                    orderName: [{ required: true, message: '请填写订单名称', trigger: 'blur' }]
                }
            };
        },
        created() {
			 this.getcompany();
            let that = this;
          //  let localCompanyFrom = storage.getItem('groupOrder_companyFrom');
            // if (localCompanyFrom) {
            //     localCompanyFrom = JSON.parse(localCompanyFrom);
            //     for (let k in localCompanyFrom) {
            //         that.companyFrom[k] = localCompanyFrom[k];
            //     }
            //     that.canSave = false;
            // }
        },
        methods: {

         getcompany(){
			let customerId=storage.getItem('customerId');
			 
			getAction(domain.publicUrl +this.url.companydetail,{id:customerId}).then((res)=>{
			         if(res.data.code==0){
			           this.companyFrom.customerName=res.data.data.customerName;
					   this.companyFrom.referred=res.data.data.referred;
					    this.companyFrom.remark=res.data.data.remark;
					   this.canSave = false;
			         }else{
			         
			         }
			       })
		 },
          
			updatecompany(){
				  let that = this;
				let formData={
					customerId: storage.getItem('customerId'),
					customerName: this.companyFrom.customerName,
					referred: this.companyFrom.referred,
					remark: this.companyFrom.remark,
				};
				
			postAction(domain.publicUrl +this.url.updatecompany,formData).then((res)=>{
			        if(res.success){
			          this.$message.success(res.data.data);
					 
					   storage.setItem('groupOrder_companyFrom',JSON.stringify(that.companyFrom));
			        }else{
			          this.$message.success(res.data.data);
			        }
			      })
			
			},
			
            submitForm(formName) {
                let that = this,
                    companyFrom = that.companyFrom;
                that.$refs[formName].validate(valid => {
                    if (valid) {
                        addcustomer({
							 'orderSn': storage.getItem('orderSn'),
                            token: storage.getItem('token'),
                            isCompany: 1,
                            customerName: companyFrom.customerName,
                            referred: companyFrom.referred,
                            remark: companyFrom.remark
                        }).then(res => {
                            if (res.status == 200 && res.data.code == 0) {
                                storage.setItem('customerId', res.data.msg);
                                storage.setItem('groupOrder_companyFrom', JSON.stringify(that.companyFrom));
                                this.$message.success("新增客户成功！")
								this.canSave = false;
                            }else {
                                this.$message.error(res.data.data)
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
                /* addcustomer({}).then(res=>{
                              console.log(res);
                          }) */
            }
        }
    };
</script>

<style lang="scss"></style>
